package com.feimt.common;

import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.*;
import com.aliyun.teautil.models.*;
import com.aliyun.dysmsapi20170525.Client;

/**
 * @author 宋孟林
 * @description
 * @date 2025-3-4
 */
public class SMSSendCode {
    private static final String ACCESS_KEY_ID = "LTAI5tHznXb2JwrG642A2asH";
    /**
     * 密钥
     */
    private static final String ACCESS_KEY_SECRET = "kxC5JkjPQ3NdqrUACHe4nRITVVjwAC";
    /**
     * sign_name
     */
    private static final String SIGN_NAME = "阿里云短信测试";
    /**
     * 短信模板
     */
    private static final String TEMPLATE_CODE = "SMS_154950909";
    /**
     * 域名
     */
    private static final String ENDPOINT ="dysmsapi.aliyuncs.com";

    /**
     * 发送短信
     * @param phone 手机号
     * @param code 验证码
     */
    public static void sendCode(String phone,String code) throws Exception {
        Client client = SMSSendCode.createClient(
                ACCESS_KEY_ID,
                ACCESS_KEY_SECRET);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(SIGN_NAME)
                .setTemplateCode(TEMPLATE_CODE)
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":"+code+"}");
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
        com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
    }
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = ENDPOINT;
        return new Client(config);
    }
    public static void main(String[] args) {
        //1.生成一个验证码
        Integer code = CodeUtils.generateValidateCode(4);
        System.out.println("code = " + code);
        //2. 调用发短信
        try {
            SMSSendCode.sendCode("18162161837", code.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}